package com.hjxy.controller;


import com.hjxy.pojo.Teacher;
import com.hjxy.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: zhangjie
 * @createDate: 2020/10/3
 * @version: 1.0
 */

@Controller
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @GetMapping("/teacher1")
    public String teacher(){
        //直接返回jsp页面
        return "teacher";
    }
    //查询所有教师信息
    @RequestMapping("/queryTeacher")
    @ResponseBody
    public Map<String, Object> getList(){
        List<Teacher> teachers = teacherService.queryTeacher();
        System.out.println(teachers+"<==teachers");
        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("msg", "获取数据成功");
        result.put("teachers", teachers);
        return result;
    }


    //跳转到新增教师信息页面
    @RequestMapping("/addTeacher")
    public String addTeacher(){
        return "teacher-add";
    }

    //新增教师信息功能
    @RequestMapping("/insertTeacher")
    @ResponseBody
    public Map<String,Object> insertTeacher(Teacher teacher){
        int i = teacherService.addTeacher(teacher);
        System.out.println(teacher.getTeacherId()+"==========id");
        System.out.println(teacher.getTeacherName()+"=========name");
        Map<String,Object> result = new HashMap<>();
        result.put("code",0);
        result.put("msg","新增成功");
        result.put("teacher",teacher);
        return result;
    }

    //修改教师信息
    @RequestMapping("/update")
    @ResponseBody
    public Map<String,Object> updateteacher(Integer teacherId){
        System.out.println("teacherId1==>"+teacherId);
        Teacher teacher = teacherService.queryOneTeacherById(teacherId);
        System.out.println("teacherId2==>"+teacherId);
        System.out.println("teacher==>"+teacher);
        Map<String,Object> result = new HashMap<>();
        if (teacher!=null){
            result.put("code",0);
            result.put("message","成功");
            result.put("teacher",teacher);
        }else {
            result.put("code",-1);
            result.put("message","失败");
        }
        return result;
    }

    //删除教师信息功能
    @RequestMapping("/deleted")
    @ResponseBody
    public Map<String,Object> deleteTeacher(Integer teacherId){
        teacherService.deleteOneTeacher(teacherId);
        System.out.println(teacherId);
        Map<String,Object> result = new HashMap<>();
        result.put("code",0);
        result.put("msg","删除");
        System.out.println(result);
        return result;
    }

}
